package com.suyafei.cofig;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: springboot-rabbitmq
 * @description
 * @author: luffy
 * @create: 2020-11-14 22:45
 */
@Configuration
public class RabbitConfig {
    @Bean
    public Queue getTTLQueue() {
        Map<String, Object> props = new HashMap<>();
        int ttl =  30000;
        props.put("x-message-ttl",ttl);
        props.put("x-dead-letter-exchange", "ex.pay.waiting");
        props.put("x-dead-letter-routing-key","pay-waiting");

        Queue tllQueue = new Queue("q.pay.ttl-waiting", false, false, false, props);

        return tllQueue;
    }

    @Bean
    public Queue queueWaiting() {
        Queue queue = new Queue("q.pay.waiting", false, false, false);
        return queue;
    }

    @Bean
    public Exchange exchangeTTLWaiting() {
        DirectExchange exchange = new DirectExchange("ex.pay.ttl-waiting", false, false);
        return exchange;
    }

    @Bean
    public Exchange exchangeWaiting() {
        DirectExchange directExchange = new DirectExchange("ex.pay.waiting", false, false,null);
        return directExchange;
    }

    @Bean
    public Binding bindingTTLWaiting() {
        return BindingBuilder.bind(getTTLQueue()).to(exchangeTTLWaiting()).with("pay.ttl-waiting").noargs();
    }

    @Bean
    public Binding bindingWaiting() {
        return BindingBuilder.bind(queueWaiting()).to(exchangeWaiting()).with("pay-waiting").noargs();
    }
}
